//
// Created by Semir on 2020/5/12.
//

#include <iostream>

using namespace std;

struct ListNode {
    int val;
    struct ListNode *next;
    explicit ListNode(int x) : val(x), next(nullptr) {}
};

ListNode* EntryNodeOfLoop(ListNode* pHead) {
    if (pHead == nullptr) {
        return nullptr;
    }
    bool hasLoop = false;
    ListNode* fNode = pHead;
    ListNode* sNode = pHead;
    while (fNode->next != nullptr) {
        fNode = fNode->next;
        sNode = sNode->next;
        if (fNode->next) {
            fNode = fNode->next;
        }
        if (fNode->next == sNode) {
            hasLoop = true;
            break;
        }
    }
    if (!hasLoop) {
        return nullptr;
    }
    int cnt = 1;
    ListNode* mark = sNode;
    while (sNode->next != mark) {
        sNode = sNode->next;
        cnt++;
    }
    fNode = pHead;
    while (cnt) {
        fNode = fNode->next;
        cnt--;
    }
    sNode = pHead;
    while (fNode != sNode) {
        fNode = fNode->next;
        sNode = sNode->next;
    }
    return sNode;
}

int main() {

    return 0;
}